﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO.Compression;
using System.Xml;

namespace server
{
    class XML_Job
    {
        private string name;
        private ArrayList members = new ArrayList();
        private string filepath;

        public XML_Job(string path)
        {
            this.filepath = path;
        }

        /// <summary>
        /// 建立XML 文件
        /// </summary>
        /// <returns></returns>
        public bool Creat()
        {
            try
            {
                StreamWriter sw = File.CreateText(filepath);
                sw.Close();
                sw = new StreamWriter(filepath , false, Encoding.GetEncoding("gb2312"));
                string head = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "\r\n" + "<config>\r\n" + "</config>";
                sw.WriteLine(head);
                sw.Flush();
                sw.Close();
                return true;
            }
            catch (System.Exception e)
            {
                return false;
            }          
        }
        /// <summary>
        /// 新建一个用户工作记录
        /// </summary>
        /// <param name="member">名称</param>
        /// <param name="job">工作名称</param>
        /// <param name="Pr0jectID">参加工程ID</param>
        /// <returns></returns>
        public bool New(string member,string job,string Pr0jectID)
        {
            try
            {
                name = member;
                DocXML myxml = new DocXML();
                myxml.FilePath = filepath;
                myxml.OpenXML();

                myxml.AddParentNode(name);
                string path = "/config/" + name;
                //string path =  name;
                myxml.SetXmlNodeValue(path, "");

                myxml.AddChildNode(path, "Name");
                string path1 = path + "/Name";
                myxml.SetXmlNodeValue(path1, member);

                myxml.AddChildNode(path, "Job");
                path1 = path + "/Job";
                myxml.SetXmlNodeValue(path1, job);

                myxml.AddChildNode(path, "ProjectID");
                path1 = path + "/ProjectID";
                myxml.SetXmlNodeValue(path1, Pr0jectID);

                myxml.SaveXmlDocument();

                return true;
            }
            catch
            {
                return false;
            }
        }

       /// <summary>
       /// 得到某人的工作名称
       /// </summary>
       /// <param name="member">姓名</param>
       /// <returns></returns>
        public string GetJob(string member)
        {
            DocXML myxml = new DocXML();
            myxml.FilePath = filepath;
            myxml.OpenXML();
            string path = "/config/" + member + "/Job";
            string job;
            try
            {
                job = myxml.GetXmlNodeValue(path);
                return job;
            }
            catch
            {
                return "wrong";
            }
        }

       /// <summary>
       /// 得到项目名称
       /// </summary>
       /// <param name="member">姓名</param>
       /// <returns></returns>
        public string GetProject(string member)
        {
            DocXML myxml = new DocXML();
            myxml.FilePath = filepath;
            myxml.OpenXML();
            string path = "/config/" + member + "/ProjectID";
            string job;
            try
            {
                job = myxml.GetXmlNodeValue(path);
                return job;
            }
            catch
            {
                return "wrong";
            }
        }


       /// <summary>
       /// 得到所有人员姓名列表
       /// </summary>
       /// <returns></returns>
        public ArrayList GetName()
        {
            XmlTextReader reader = new XmlTextReader((filepath));
            members.Clear();
            try
            {
                string s = "";
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            s = reader.Name;
                            break;
                        case XmlNodeType.Text:
                            if (s.Equals("Name"))
                            {
                                members.Add(reader.Value);
                            }

                            break;
                    }
                }
                if (reader != null)
                    reader.Close();
                return members;
            }
            catch
            {
                return members;
            }
        }

        /// <summary>
        /// 删除某个人员的工作信息
        /// </summary>
        /// <param name="member">姓名</param>
        /// <returns></returns>
        public bool Delete(string member)
        {
            try
            {
                name = member;
                DocXML myxml = new DocXML();
                myxml.FilePath = filepath;
                myxml.OpenXML();
                myxml.DeleteXmlNode(name);
                myxml.SaveXmlDocument();

                return true;
            }
            catch
            {
                return false;
            }
        }


    }
}
